﻿using Microsoft.Extensions.Hosting;
using QueueUtils.Models;
using System.Threading.Channels;

namespace QueueUtils
{
    public class MsgService : BackgroundService
    {
        private readonly Channel<MessageData> _channel;

        public MsgService(Channel<MessageData> channel)
        {
            _channel = channel;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (await _channel.Reader.WaitToReadAsync(stoppingToken))
            {
                var recData = await _channel.Reader.ReadAsync(stoppingToken);
                Console.WriteLine($"消息处理开始：{recData.MsgTitle}--{recData.Msg}");
                await Task.Delay(3000, stoppingToken);
                Console.WriteLine($"消息处理结束");
            }
        }
    }
}
